<template>
  <div style="background:#ffffff;">
    <Row type="flex" justify="center" style="margin-top: 300px;">
      <Cell :width="8">
        <Form ref="form" :validOnChange="validOnChange" :showErrorTip="showErrorTip" :labelWidth="110"
          :rules="validationRules" :model="model">
          <FormItem label="账 号" prop="name">
            <Input type="text" v-model="model.loginId" @keyup.enter="submit" />
          </FormItem>
          <FormItem label="密 码" prop="loginPassword">
            <Input type="password" v-model="model.loginPassword" @keyup.enter="submit" />
          </FormItem>
          <FormItem>
            <Button color="primary" @click="submit" :block="true">登录</Button>
          </FormItem>
          <!-- <FormItem>
            <Button @click="registerModel = true">注册</Button>
          </FormItem> -->
        </Form>
      </Cell>
      <!-- <Modal v-model="registerModel" :closeOnMask="false" hasCloseIcon="true" hasDivider="true">
        <template #header>注册</template>
<Form>
  <FormItem label="账号">
    <Input type="text" v-model="reg.account" placeholder="英文名称" />
  </FormItem>
  <FormItem label="用户姓名">
    <Input type="text" v-model="reg.name" placeholder="中文昵称" />
  </FormItem>
  <FormItem label="手机号">
    <Input type="text" v-model="reg.phone" @keyup.enter="register" />
  </FormItem>
  <FormItem label="密码">
    <Input type="password" v-model="reg.pass" @keyup.enter="register" />
  </FormItem>
</Form>
<template #footer>
          <Button color="blue" @click="register" :block="true">注册</Button>
        </template>
</Modal> -->
    </Row>
  </div>
</template>
<script>
import { sm3 } from 'sm-crypto';
import { message } from 'heyui';

export default {
  created() {

  },
  mounted() { },
  data() {
    return {
      registerModel: false,
      model: {
        loginId: "",
        loginPassword: ""
      },
      reg: {},
      validationRules: {
        required: ["name", "loginPassword"]
      },
      showErrorTip: true,
      validOnChange: true
    };
  },
  methods: {
    submit() {
      this.$store.$reset();
      let validResult = this.$refs.form.valid();
      if (validResult.result) {
        const hash = sm3(this.model.loginPassword).toUpperCase();
        this.$post("/system/doLogin", {
          deviceCode: "ollama-ai-mg",
          loginType: 1,
          loginId: this.model.loginId,
          loginPassword: hash
        }).then(res => {
          if (res.data.code == this.$success) {
            this.$store.setLoginFlag(true);
            this.$store.setLoginInfo({
              loginId: res.data.data.userId,
              loginName: res.data.data.userName,
              token: res.data.data.token,
              expires: res.data.data.expires,
            });
            location.href = "/";
          } else {
            message.error(res.data.msg);
          }
        })
          .catch(err => {
            message.error(err);
          });
      }
    },
    /* register() {
      let sm3 = new Sm3();
      sm3.update(this.reg.pass);

      this.$post("/register", {
        account: this.reg.account,
        name: this.reg.name,
        phone: this.reg.phone,
        pass: toHex(sm3.finalize()).toUpperCase()
      })
        .then(res => {
          if (res.data.code == this.$success) {
            message.success(res.data.msg);
            this.registerModel = false;
          } else {
            message.error(res.data.msg);
          }
        })
        .catch(err => {
          message.error(err);
        });
    } */
  }
};
</script>
